<template>
  <div class="password-reset-container">
    <div class="password-reset-card">
      <h2 class="password-reset-title">修改密码</h2>
      <el-form
          :model="data.user"
          ref="formRef"
          :rules="rules"
          label-width="120px"
          label-position="right"
          class="password-reset-form"
      >
        <el-form-item label="新密码" prop="newPassword">
          <el-input
              show-password
              v-model="data.user.newPassword"
              autocomplete="off"
              placeholder="请输入新密码"
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="new2Password">
          <el-input
              show-password
              v-model="data.user.new2Password"
              autocomplete="off"
              placeholder="请再次输入新密码"
          />
        </el-form-item>
        <div class="form-footer">
          <el-button
              type="primary"
              size="large"
              round
              @click="save"
          >
            确认修改
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {reactive, ref} from "vue";
import request from "@/utils/request";
import {ElMessage} from "element-plus";

const data = reactive({
  user: JSON.parse(localStorage.getItem('user') || '{}')
});

const rules = reactive({
  newPassword: [
    {required: true, message: '请输入新密码', trigger: 'blur'},
  ],
  new2Password: [
    {required: true, message: '请确认新密码', trigger: 'blur'},
  ],
});

const formRef = ref();

const save = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      request.post('/admin/updatePassword', data.user).then(res => {
        if (res.code === '200') {
          ElMessage.success('修改成功');
          location.href = '/login';
        } else {
          ElMessage.error(res.msg);
        }
      });
    }
  });
};
</script>

<style scoped>
.password-reset-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #eceef1, #9c9d9f, #49494a);
  padding: 20px;
}

.password-reset-card {
  background: #fff;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 500px;
  text-align: center;
}

.password-reset-title {
  margin-bottom: 20px;
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.password-reset-form {
  margin-top: 20px;
}

.el-input {
  border-radius: 6px;
}

.form-footer {
  margin-top: 30px;
}

.el-button {
  padding: 12px 32px;
  font-size: 16px;
  background: linear-gradient(90deg, #c4c7cc, #3b3b3c);
  border: none;
  color: #fff;
  box-shadow: 0 4px 8px rgba(67, 100, 247, 0.3);
  transition: all 0.3s ease;
}

.el-button:hover {
  background: linear-gradient(90deg, #3b3b3c, #c4c7cc);
  box-shadow: 0 6px 12px rgba(67, 100, 247, 0.4);
}
</style>
